<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/20
 * Time: 下午7:35
 */
class SceCustomController extends BaseController {
    /*
     * 技师更新并认证车辆
     * */
    public function confirm_user_car_info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_no' => 'required',
                'brand' => 'required',
                'series' => 'required',
                'model' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $car_no = Input::get('car_no');
            $brand = Input::get('brand');
            $series = Input::get('series');
            $model = Input::get('model');
            $vin = strtoupper(Input::get('vin',''));
            $car_id = Input::get('car_id');
            $note = Input::get('note','');
            $mileage = Input::get('mileage',0);

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            try{
                //先update 设置状态
//                if(!is_null($car_id)){
                    // UserCarInfo::where('car_no',$car_no)->update(array('status' => 0));//更新 车型不为 现在车型的 认证车辆为 未认证
                UserCarInfo::where("car_no",Input::get("car_no"))->where("is_lock",1)->where("car_model_id","!=",$model)->update(array("is_lock"=>0));  //更新 车型不为 现在车型的 认证车辆为 未认证
//                }
                $user_car_info = new UserCarInfo();
                $user_car_info->user_id = 0;
                $user_car_info->status = 1;
                $user_car_info->car_brand_id = $brand;
                $user_car_info->car_series_id = $series;
                $user_car_info->car_model_id = $model;
                $user_car_info->is_default = 0;
                $user_car_info->car_level = 0;
                $user_car_info->is_lock = 1;
                $user_car_info->car_no = $car_no;
                $user_car_info->vin_code = $vin;
                $user_car_info->mileage = $mileage;
                $user_car_info->note = $note;
                $user_car_info->updated_at = Common::num2DateTime(time());
                $user_car_info->created_at = Common::num2DateTime(time());
                $user_car_info->save();

                $result = array(
                    'car_id' => $user_car_info->id,
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "sce" ,"event" => "update_car_info", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("sce_error.order_update_error_1009");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "update_car_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询用户通过车辆
     * */
    public function search_by_car(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_no' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $car_no = Input::get('car_no');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }
            //获取绑定该车辆的用户信息
            $users = UserCarInfo::join('users','users.id','=','user_car_info.user_id')
                ->where('user_car_info.car_no',$car_no)
                ->where('users.status',1)
                ->select(array('users.id','users.real_name as name','users.phone'))
                ->get()->toArray();

            $count = count($users);
            if($count == 0){
                $users = array(
                    'id' => 0,
                    'name' => '未知',
                    'phone' => '未知'
                );
            }
            $result = array(
                'count' => $count,
                'users' => $users
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "search_by_car", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询用户通过手机
     * */
    public function search_by_phone(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'phone' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $phone = Input::get('phone');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }
            //获取绑定该车辆的用户信息
            $user = Users::where('phone',$phone)
                ->where('status',1)
                ->select(array('id','real_name as name','phone'))
                ->get()->toArray();

            $result = array(
                'user' => $user
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "search_by_phone", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}